package org.shj.basic;

/**
 * volatile 并不能保证线程安全
 * 
 * @author huangjian
 *
 */
public class VolatileTest {

	public static volatile int race = 0;
	
	public static void increase(){
		race++;
	}
	
	private static final int THREAD_COUNT = 20;
	
	public static void main(String[] args){
		for(int i = 0 ; i < THREAD_COUNT; i++){
			new Thread(new Runnable(){
				public void run(){
					for(int i = 0 ; i < 10000; i++){
						increase();
					}
				}
			}); 
		}
		
		//等待所有累加线程都结束
		while(Thread.activeCount() > 1)
			Thread.yield();
		
		System.out.println(race);
	}
}
